<template>
  <div class="tools-li">
    <transition name="el-zoom-in-right">
      <div
        v-show="isShow"
        :key="tooltipId"
        ref="customAlert"
        class="custom-demo"
        :style="customDemo"
        transiton="el-zoom-in-center"
      >
        <div v-if="edit=== false" class="custom-ul-li" @click="editTheme(true)">
          修改布局
        </div>
        <div v-if="edit=== true" class="custom-ul-li" @click="editTheme(false)">
          取消修改
        </div>
        <div class="custom-ul-li" @click="saveTheme">
          保存设置
        </div>
        <div class="custom-ul-li" @click="reset">
          恢复默认
        </div>
      </div>
    </transition>
    <slot name="theme" />
  </div>
</template>

<script>
import { generateId } from 'element-ui/src/utils/util'
export default {
  name: 'Setting',
  props: {
    trigger: {
      type: String,
      default: 'click',
      validator: value => ['click', 'focus', 'hover', 'manual'].indexOf(value) > -1
    }
  },
  data() {
    return {
      isShow: false,
      customDemo: {},
      customMenu: {},
      mouseId: null,
      edit: false
    }
  },
  computed: {
    tooltipId() {
      return `el-custom-user-${generateId()}`
    }
  },
  mounted() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.addEventListener('click', this.triggerOnClick)
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.addEventListener('mouseenter', this.triggerOpen)
      this.$refs.customAlert.addEventListener('mouseleave', this.triggerClose)
    }
  },
  destroyed() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.removeEventListener('click', this.triggerOnClick)
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.removeEventListener('mouseenter', this.triggerOpen)
      this.$refs.customAlert.removeEventListener('mouseleave', this.triggerClose)
    }
  },
  methods: {
    triggerOnClick() {
      if (this.isShow) {
        this.isShow = false
      } else {
        this.isShow = true
      }
    },
    triggerOpen() {
      this.isShow = true
      this.$emit('callBack', this.isShow)
    },
    triggerClose() {
      this.isShow = false
      this.$emit('callBack', this.isShow)
    },
    mousemove(data) {
      this.mouseId = data.id
    },
    mouseleave() {
      this.mouseId = null
    },
    editTheme(data) {
      this.edit = data
      this.$bus.emit('editTheme', data)
    },
    saveTheme() {
      this.$bus.emit('saveTheme')
    },
    reset() {
      this.$bus.emit('resetTheme')
    }
  }
}
</script>

<style scoped>
.tools-li {
  width: 100%;
  height: 100%;
  position: relative;
}
.custom-demo {
  position: absolute;
  top: -74px;
  right: 67px;
  height: auto;
  padding: 10px 0;
  box-sizing: border-box;
  width: 120px;
  z-index: 100;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 4px;
  background-color: #ffffff;
}
.custom-ul-li {
  width: 100%;
  height: 40px;
  line-height: 40px;
  font-size: 0.8rem;
  cursor: pointer;
  text-align: center;
  background: #fff;
  color:black;
  padding: 0 10px;
  box-sizing: border-box;
}
.custom-ul-li.active {
  background-color: #e4e4e4;
}
.custom-ul-li:hover {
  background-color: #e1e1e1;
}
</style>
